class Solution {
public:
  // 遇到数字则入栈；遇到算符则取出栈顶两个数字进行计算，并将结果压入栈中
    int evalRPN(vector<string>& tokens) {
        stack<int> num;
        for(int i = 0; i < tokens.size(); ++i){
            string& s = tokens[i];
            if(s == "+" || s == "-" || s == "*" || s == "/" ){
                // 是操作符，对栈顶连续两个数进行运算
                int num1 = num.top(), num.pop();
                int num2 = num.top(), num.pop();
                char operator = s[0];
                switch(operator){
                    case '+':  num.push(num2 + num1);
                        break;
                    case '-':  num.push(num2 - num1);
                        break;
                    case '*':  num.push(num2 * num1);
                        break;
                    case '/':  num.push(num2 / num1);
                        break;
                    default : break;
               }
            }   
            else num.push(stoi(s));  // 数字入栈(string to int)
        }
        return num.top();
    }
};